package com.aramis.mydytt.widget

import android.app.Activity
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.core.content.ContextCompat
import com.aramis.mydytt.R

/**
 * Description: TitleBar
 * @author lizhidan
 * Created: 2021/6/7
 */
class TitleBar : RelativeLayout {
    constructor(context: Context?) : super(context) {
        init(null, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
        init(attrs, 0)
    }

    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(attrs, defStyleAttr)
    }

    private lateinit var backImageView: AppCompatImageView
    private lateinit var titleView: TextView
    private lateinit var rightOptionView: TextView

    private fun init(attrs: AttributeSet?, defStyleAttr: Int) {
        inflate(context, R.layout.title_common, this)
        setBackgroundColor(ContextCompat.getColor(context, R.color.app_bg_dark))
        backImageView = findViewById(R.id.image_title_common_back)
        titleView = findViewById(R.id.text_title_common)
        rightOptionView = findViewById(R.id.text_title_common_right)
        backImageView.setOnClickListener {
            if (context is Activity) {
                (context as Activity).onBackPressed()
            }
        }
    }

    fun setTitle(title: String) {
        titleView.text = title
    }

    fun setTitle(strId: Int) {
        titleView.setText(strId)
    }

    fun setRightText(strId: Int) {
        rightOptionView.setText(strId)
    }

    fun setRightTextViewVisibility(visible: Int) {
        rightOptionView.visibility = visible
    }

    fun setOnRightTextOnClickListener(listener: OnClickListener) {
        rightOptionView.setOnClickListener(listener)
    }
}